<?php

namespace app\sys\controller;

use think\Db;

// 后台 财务管理
class Finance extends Admin
{
    // 财务报表（跑定时任务 每天0点生成）
    public function index()
    {
        if (request()->isAjax()) {
            $date = input('param.date', '');
            $page = input('param.page', 1);
            $page = $page <= 0 ? 1 : $page;
            $limit = input('param.limit', 20);
            $order_field = input('param.field', '');
            $order_type = input('param.order', 0);
            $query = [];
            if ($date) {
                $arr = explode('~', $date);
                $query['ct'] = [['gte', strtotime(trim($arr[0]))], ['lte', strtotime(trim($arr[1]).'23:59:59')]];
            }
            $order = ['ct' => -1];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $list = Db::name('finance')->where($query)->page($page)->limit($limit)->order($order)->select();
            foreach ($list as $key => $value) {
                $list[$key]['ct'] = date('Y-m-d', $value['ct']);
            }
            $count = Db::name('finance')->where($query)->count();

            return success(['data' => $list, 'count' => $count]);
        }

        return view('index');
    }

    // 业务员提成管理
    public function csmanager()
    {
        if (request()->isAjax()||request()->isPost()) {
            $admin_info = session('admin_info');
            $date = input('param.date', '');
            $page = input('param.page', 1);
            $page = $page <= 0 ? 1 : $page;
            $limit = input('param.limit', 20);
            $order_field = input('param.field', '');
            $order_type = input('param.order', 0);
            $query = [];
            if ($date) {
                $arr = explode('~', $date);
                $query['ct'] = [['gte', strtotime(trim($arr[0]))], ['lte', strtotime(trim($arr[1]).'-01 23:59:59 +1 month -1 day')]];
            } else {
                $month = date('Y-m');
                $query['month'] = $month;
            }
            // 业务员只能看到自己的
            if (3 == $admin_info['permission']) {
                $query['csid'] = $admin_info['id'];
            }
            $order = ['ct' => -1];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $list = Db::name('cs_push_money')->where($query)->page($page)->limit($limit)->order($order)->select();
            foreach ($list as $key => $value) {
                $a_info = Db::name('admin')->find($value['csid']);
                $list[$key]['username'] = $a_info['username'];
                $list[$key]['ct'] = date('Y-m-d', $value['ct']);
                $list[$key]['qrcode'] = $a_info['qrcode'];
            }
            $count = Db::name('cs_push_money')->where($query)->page($page)->limit($limit)->order($order)->count();

            return success(['data' => $list, 'count' => $count]);
        }

        return view('csmanager');
    }

    // 供应商 财务明细
    public function detail()
    {
        if (request()->isAjax()) {
            $admin_info = session('admin_info');
            $date = input('param.date', '');
            $page = input('param.page', 1);
            $page = $page <= 0 ? 1 : $page;
            $limit = input('param.limit', 20);
            $order_field = input('param.field', '');
            $order_type = input('param.order', 0);
            $query = [];
            // 供应商只能看到自己的
            if (6 == $admin_info['permission']) {
                $query['admin_id'] = $admin_info['id'];
            }
            if ($date) {
                $arr = explode('~', $date);
                $query['ct'] = [['gte', strtotime(trim($arr[0]))], ['lte', strtotime(trim($arr[1]).'23:59:59')]];
            }
            $order = ['ct' => -1];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $list = Db::name('finance_detail')->where($query)->page($page)->limit($limit)->order($order)->select();
            foreach ($list as $key => $value) {
                $list[$key]['ct'] = date('Y-m-d H:i:s', $value['ct']);
            }
            $count = Db::name('finance_detail')->where($query)->count();

            return success(['data' => $list, 'count' => $count]);
        }

        return view('detail');
    }
}
